package com.ungs.form;

import java.awt.Frame;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.ungs.utils.Constantes;

class RadioListener implements ActionListener {
	@SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent e) {
		try {
			UIManager.setLookAndFeel(e.getActionCommand());
			refreshAll();
			Properties p = new Properties();
			p.load(new FileInputStream(Constantes.CONF_INI));

			p.setProperty("laf", e.getActionCommand());
			FileOutputStream out = new FileOutputStream(Constantes.CONF_INI);
			p.save(out, "config.ini_Change");
			
		} catch (ClassNotFoundException ex) {
			// TODO Auto-generated catch block
			ex.printStackTrace();
		} catch (InstantiationException ex) {
			// TODO Auto-generated catch block
			ex.printStackTrace();
		} catch (IllegalAccessException ex) {
			// TODO Auto-generated catch block
			ex.printStackTrace();
		} catch (UnsupportedLookAndFeelException ex) {
			// TODO Auto-generated catch block
			ex.printStackTrace();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
	}

	private void refreshAll() {
		Frame frames[] = Frame.getFrames();

		// refresh all Frames in the app
		for (int i = 0; i < frames.length; i++) {
			SwingUtilities.updateComponentTreeUI(frames[i]);
			Window windows[] = frames[i].getOwnedWindows();

			// refresh all windows and dialogs of the frame
			for (int j = 0; j < windows.length; j++) {
				SwingUtilities.updateComponentTreeUI(windows[j]);
			}
		}
	}
}